# -*- coding: utf-8 -*-
import math
def round(x):
    if isinstance(x, float):
        if x-math.floor(x)>0.5:
            x = math.floor(x)+1
        else:
            x = math.floor(x)
        return x
    if isinstance(x, complex):
        re = x.real
        im = x.imag
        if re-math.floor(re)>0.5:
            re = math.floor(re)+1
        else:
            re = math.floor(re)
        if im-math.floor(im)>0.5:
            im = math.floor(im)+1
        else:
            im = math.floor(im)
        return complex(re, im)    
def cub_eq(g, a, b, c):#находит корни g*x^3+a*x^2+b*x+c=0 (возможна погрешность!)
    g = float(g)
    if g == 0:
       raise ValueError("Not cubic equation")
    a/=g
    b/=g
    c/=g
    Q = (a*a-3*b)/9.0
    R = (2*a**3 - 9*a*b + 27*c)/54.0
    S = Q**3 - R**2
    x = []
    if S>0:
        fi = math.acos(R/math.sqrt(Q**3))/3.0
        x.append(-2*math.sqrt(Q)*math.cos(fi)-a/3.0)
        x.append(-2*math.sqrt(Q)*math.cos(fi+2*math.pi/3.0)-a/3.0)
        x.append(-2*math.sqrt(Q)*math.cos(fi-2*math.pi/3.0)-a/3.0)
    if S<0:
        if Q>0:
            fi = math.acosh(abs(R)/math.sqrt(Q**3))/3.0
            x.append(-2*math.copysign(1, R)*math.sqrt(Q)*math.cosh(fi)-a/3.0)
            x.append(complex(math.copysign(1, R)*math.sqrt(Q)*math.cosh(fi)-a/3.0, math.sqrt(3)*math.sqrt(Q)*math.sinh(fi)))
            x.append(complex(x[-1].real, -x[-1].imag))
        if Q<0:
            Q=-Q
            fi = math.asinh(abs(R)/math.sqrt(Q**3))/3.0
            x.append(-2*math.copysign(1, R)*math.sqrt(Q)*math.sinh(fi)-a/3.0)
            x.append(complex(math.copysign(1, R)*math.sqrt(Q)*math.sinh(fi)-a/3.0, math.sqrt(3)*math.sqrt(Q)*math.cosh(fi)))
            x.append(complex(x[-1].real, -x[-1].imag))
        if Q==0:
            x.append(abs(c)**(1/3.0)*math.copysign(1, c))
            x = x*3            
    if S==0:
        x.append(-2*(R**(1/3.0))-a/3.0)
        x.append((R**(1/3.0))-a/3.0)
        x.append(x[-1])
    for i in xrange(0, 3):
        r = round(x[i])
        if r**3+a*r**2+b*r+c==0:
            x[i]=r
    return x
#print cub_eq(-1, 3, -7, 5)
#print cub_eq(1, 6, 11, 6)
    
